package abacab.ref;

import abacab.ActorSystem;

/**
 * The generic actor finder for local actors
 */
public class ActorLocatorImpl implements ActorLocator {
	@Override
	public <M> ActorRef<M> locateActor(String path) throws IllegalArgumentException {
		if (!path.startsWith("/")) {
			return null;
		}
		int idx = path.indexOf('/', 1);
		if (idx < 0) {
			throw new IllegalArgumentException("The path does not represent a local actor correctly: " + path);
		}
		String systemName = path.substring(1, idx);
		ActorSystem system = ActorSystem.getActorSystemIfExists(systemName);
		if (system == null) {
			// Unknown system for this classloader
			return null;
		}
		String reference = path.substring(idx + 1);
		return system.getActor(reference);
	}
}